package com.wu.common.module.ui.activity;


import android.content.Intent;
import android.os.Bundle;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.wu.common.R;
import com.wu.common.databinding.ActivityUserTipsBinding;

import com.wu.common.module.ui.WebViewActivity;
import com.wu.common.utils.SharedPreferencesHelper;


public class UserTipsActivity extends AppCompatActivity {

    private ActivityUserTipsBinding binding;
    private String protocol = "https://wjj.ys1.cnliveimg.com/term/witnessRzxy.html";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_user_tips);
        initView();
    }

    /**
     * 初始化页面
     */
    private void initView() {

        String textContent =
                getString(R.string.user_tips);

        //-------------------------------------------------
        //登录即代表阅读并同意《用户协议》 及 《隐私政策》
        SpannableStringBuilder ssb = new SpannableStringBuilder(textContent);
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                WebViewActivity.Companion.newInstance(UserTipsActivity.this, "https://htmlpreview.github.io/?https://github.com/wukuiqing49/app_comment/blob/master/xy/yhxy.html", "用户协议");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        }, 48, 54, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
//                WebViewPActivity.Companion.newInstance(UserTipsActivity.this, "ys", "隐私协议");
                WebViewActivity.Companion.newInstance(UserTipsActivity.this, "https://htmlpreview.github.io/?https://github.com/wukuiqing49/app_comment/blob/master/xy/yszc.html", "隐私政策");

            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        }, 55, 61, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_23d41e)), 48, 54, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ssb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_23d41e)), 55, 61, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        binding.text.setMovementMethod(LinkMovementMethod.getInstance());
        binding.text.setText(ssb);
        binding.tvCancel.setOnClickListener(this::cancel);
        binding.tvOk.setOnClickListener(this::ok);
        binding.tvExit.setOnClickListener(this::cancel);
        binding.tvBack.setOnClickListener(this::ok);
    }

    private void ok(View view) {
        if (binding.userTipsRoot.getVisibility() == View.VISIBLE) {

           SharedPreferencesHelper.getInstance(this).setValue("isShowTip",true);
            Intent intent=new Intent();
            intent.putExtra("isShow",true);
            setResult(10011,intent);
            finish();
        } else {
            binding.userTipsRoot.setVisibility(View.VISIBLE);
            binding.userBackRoot.setVisibility(View.GONE);
        }
    }

    private void cancel(View view) {
        if (binding.userTipsRoot.getVisibility() == View.VISIBLE) {
            binding.userTipsRoot.setVisibility(View.GONE);
            binding.userBackRoot.setVisibility(View.VISIBLE);
        } else {
            FinishAllActivity.finish(this);
        }
    }

    @Override
    public void onBackPressed() {
        if (binding.userTipsRoot.getVisibility() == View.VISIBLE) {
            binding.userTipsRoot.setVisibility(View.GONE);
            binding.userBackRoot.setVisibility(View.VISIBLE);
        } else {
            binding.userTipsRoot.setVisibility(View.VISIBLE);
            binding.userBackRoot.setVisibility(View.GONE);
        }
    }

    private void startWebView(String url) {
      // 打开网页

    }
}
